package dao;

import java.util.Collection;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import model.Cliente;
import model.ClientePF;
import model.ClientePJ;
@Stateless
public class ClienteDAO implements ClienteDAOLocal{
	@PersistenceContext
	private EntityManager entityManager;
	
	@Override
	public void atualiza(Integer id, String cpfCnpj, String end, String nomeRazaoSocial, String ddd, String tel, String bairro) {
		Cliente c = entityManager.find(Cliente.class, id);
		if (c instanceof ClientePF) {
			ClientePF cliPf = (ClientePF) c;
			cliPf.setNome(nomeRazaoSocial);
		}
		else {
			ClientePJ cliPj = (ClientePJ) c;
			cliPj.setRazao_social(nomeRazaoSocial);
		}
		c.setBairro(bairro);
		c.setDdd(ddd);
		c.setEndereco(end);
		c.setTelefone(tel);
		c = entityManager.merge(c);
			
	}
	
	@Override
	public void grava(String cpfCnpj, String end, String nome, String ddd, String tel, String bairro) {
		try {
			Cliente cli = null;
			if (cpfCnpj.length() == 11) {
				cli = new ClientePF();
			}
			else if (cpfCnpj.length() == 14) {
				cli = new ClientePJ();
			}
			
			cli.setCpfCnpj(cpfCnpj);
			cli.setEndereco(end);
			cli.setNome(nome);
			cli.setTelefone(tel);
			cli.setDdd(ddd);
			cli.setBairro(bairro);
			entityManager.persist(cli);
			
		}   catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} 
		
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection <Cliente> findClientes() {
		Collection <Cliente> clientes = entityManager.createQuery("select c from Cliente c").getResultList();
		if (clientes == null || clientes.size() == 0) {
			return null;
		}
		return clientes;
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Cliente> findClientesByTel(String num, String ddd)  throws ItemNaoEncontradoException{
		Query q = entityManager.createNamedQuery("findClienteByTel");
		q.setParameter(1, num);
		q.setParameter(2, ddd);
		Collection<Cliente> clientes =  q.getResultList();
		if (clientes == null || clientes.size() == 0 ) throw new ItemNaoEncontradoException("Cliente não encontrado.");
		return clientes;
	}

	@SuppressWarnings("unchecked")
	public Collection<Cliente> findClientesByTel(String num)  throws ItemNaoEncontradoException{
		Query q = entityManager.createNamedQuery("findClienteByTelefone");
		q.setParameter(1, "%" + num + "%");
		Collection<Cliente> clientes =  q.getResultList();
		if (clientes == null || clientes.size() == 0 ) throw new ItemNaoEncontradoException("Cliente não encontrado.");
		return clientes;
	}
	
	
	@Override
	@SuppressWarnings("unchecked")
	public Collection<Cliente> findClientesByNome(String nome)  throws ItemNaoEncontradoException{
		Query q = entityManager.createNamedQuery("findClienteByNome");
		StringBuilder sb = new StringBuilder();
		sb.append("%");
		sb.append(nome.toUpperCase());
		sb.append("%");
		q.setParameter(1,sb.toString());
		Collection<Cliente> clientes =  q.getResultList();
		if (clientes == null || clientes.size() == 0 ) throw new ItemNaoEncontradoException("Cliente não encontrado.");
		return clientes;
	}
}
